//
//  UIButton+CHExtension.m
//  YoLive
//
//  Created by Lucky on 16/9/28.
//  Copyright © 2016年 上海成红网络科技有限公司. All rights reserved.
//

#import "UIButton+CHExtension.h"

@implementation UIButton (CHExtension)

#pragma mark --- 创建默认按钮--有字体、颜色--有图片---有背景

+ (UIButton *)defaultBtnWithFrame:(CGRect)frame Title:(NSString *)title target:(id)target action:(SEL)action{
    
    UIButton *loginBtn = [[UIButton alloc] initWithFrame:frame];
    loginBtn.titleLabel.font = LGFONT(18);
    [loginBtn setTitle:title forState:UIControlStateNormal];
    [loginBtn setTitleColor:LGCOLOR(@"#FFFFFF") forState:UIControlStateNormal];
    [loginBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    loginBtn.layer.shadowColor = [UIColor colorWithRed:238/255.0 green:148/255.0 blue:7/255.0 alpha:0.35].CGColor;
    loginBtn.layer.shadowOffset = CGSizeMake(0,2);
    loginBtn.layer.shadowOpacity = 1;
    loginBtn.layer.shadowRadius = 11;
    
    CAGradientLayer *gl = [CAGradientLayer layer];
    gl.frame = CGRectMake(0,0,290,49);
    gl.startPoint = CGPointMake(0, 0);
    gl.endPoint = CGPointMake(1, 1);
    gl.colors = @[(__bridge id)[UIColor colorWithRed:255/255.0 green:184/255.0 blue:30/255.0 alpha:1.0].CGColor,(__bridge id)[UIColor colorWithRed:255/255.0 green:214/255.0 blue:70/255.0 alpha:1.0].CGColor];
    gl.locations = @[@(0.0),@(1.0)];
    gl.cornerRadius = 24.5;
    [loginBtn.layer insertSublayer:gl atIndex:0];
    
    return loginBtn;
}


+ (instancetype)buttonWithTitle:(NSString *)title titleColor:(UIColor *)titleColor font:(UIFont *)font imageName:(NSString *)imageName target:(id)target action:(SEL)action backImageName:(NSString *)backImageName  {
    
    UIButton *button = [[UIButton alloc] init];
    // 设置标题
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    button.titleLabel.font = font;
    button.adjustsImageWhenHighlighted = NO;
    // 图片
    if (imageName != nil) {
        [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        NSString *highlighted = [NSString stringWithFormat:@"%@_highlighted", imageName];
        [button setImage:[UIImage imageNamed:highlighted] forState:UIControlStateHighlighted];
    }
    
    // 背景图片
    if (backImageName != nil) {
        [button setBackgroundImage:[UIImage imageNamed:backImageName] forState:UIControlStateNormal];
        
        NSString *backHighlighted = [NSString stringWithFormat:@"%@_highlighted", backImageName];
        [button setBackgroundImage:[UIImage imageNamed:backHighlighted] forState:UIControlStateHighlighted];
    }
    
    // 监听方法
    if (action != nil) {
        [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    return button;
}

#pragma mark  --- 有文字,有颜色，有字体，有图片，没有背景图片
+ (instancetype)buttonWithTitle:(NSString *)title titleColor:(UIColor *)titleColor font:(UIFont *)font imageName:(NSString *)imageName target:(id)target action:(SEL)action {
    return [self buttonWithTitle:title titleColor:titleColor font:font imageName:imageName  target:target action:action backImageName:nil];
}


#pragma mark  --- 有文字,有颜色，有字体，没有图片，没有背景
+ (instancetype)buttonWithTitle:(NSString *)title titleColor:(UIColor *)titleColor font:(UIFont *)font target:(id)target action:(SEL)action {
    return [self buttonWithTitle:title titleColor:titleColor font:font imageName:nil target:target action:action backImageName:nil];
}

#pragma mark  --- 有文字,有颜色,有字体,没图片，有背景
+ (instancetype)buttonWithTitle:(NSString *)title titleColor:(UIColor *)titleColor font:(UIFont *)font target:(id)target action:(SEL)action backImageName:(NSString *)backImageName {
    return [self buttonWithTitle:title titleColor:titleColor font:font imageName:nil target:target action:action backImageName:backImageName];
}

+ (instancetype)buttonWithBackgroundImage:(NSString *)image target:(id)target action:(SEL)action {
    UIButton *btn = [[UIButton alloc]init];
    [btn setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [btn addTarget:target action:@selector(action) forControlEvents:UIControlEventTouchUpInside];
    
    return btn;
}
@end
